﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SQLite;

namespace WindowsForms_hw11
{
    public partial class Form1 : Form
    {
        string datasource = "../../../words_database.db";
        SQLiteConnection sqlconn = new SQLiteConnection();
        private string sql;
        private SQLiteCommand sqlcmd;
        string result;
        int id = 25;
        int id2;
        bool flag = true;
        private Random ran = new Random();
        public Form1()
        {
            InitializeComponent();
            label2.Visible = false;
            SQLiteConnectionStringBuilder connsb = new SQLiteConnectionStringBuilder();
            connsb.DataSource = datasource;
            sqlconn.ConnectionString = connsb.ToString();
            sqlconn.Open();
            sqlcmd = new SQLiteCommand();
            sqlcmd.Connection = sqlconn;
            nextWord();
        }

        private void button2_Click(object sender, EventArgs e)
        {
           
            label2.Visible = true;
            if (textBox1.Text == result)
            {
                label2.Text = "正确!";
                label2.ForeColor = Color.Green;
                nextWord();
       
            }
            else
            {
                label2.Text = "错误!";
                textBox1.Text = "";
                label2.ForeColor = Color.Red;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            MessageBox.Show("这个单词是 " + result+" ,下次要记住哦！");
            nextWord();
        }
        private void nextWord() {
            id = id % 40;
            textBox1.Text = "";
            if (flag)
            {
                sql = "SELECT * FROM words WHERE rowid = " + (++id);
            }
            else {
                do
                {
                    id2 = id;
                    id = ran.Next(1, 40);
                } while (id2 == id);
                sql = "SELECT * FROM words WHERE rowid = " + id;
            }
            sqlcmd.CommandText = sql;
            SQLiteDataReader reader = sqlcmd.ExecuteReader();
            reader.Read();
            label3.Text = reader.GetString(1);
            result = reader.GetString(0);
            reader.Close();
        }

        private void button3_MouseDown(object sender, MouseEventArgs e)
        {
            textBox1.Text = result;
        }

        private void button3_MouseUp(object sender, MouseEventArgs e)
        {
            textBox1.Text = "";
        }

        private void button4_Click(object sender, EventArgs e)
        {
            flag = !flag;
            if (flag) {
                label4.Text = "【当前为正序模式】";
            }
            else
            {
                label4.Text = "【当前为乱序模式】";
            }
        }
    }
}
